﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 三国杀
{
    public partial class frmRegister : Form
    {
        public string QQNumber { get; private set; }
        DB db = new DB();
        public frmRegister()
        {
            InitializeComponent();
            this.SetStyle(
        ControlStyles.OptimizedDoubleBuffer |
        ControlStyles.AllPaintingInWmPaint |
        ControlStyles.UserPaint,
        true);
            this.FormClosing += FrmRegister_FormClosing;
        }

        private void FrmRegister_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 当用户点击窗口自带的关闭按钮时
            if (e.CloseReason == CloseReason.UserClosing)
            {
                // 设置DialogResult为Cancel，这样登录窗口知道是取消注册
                this.DialogResult = DialogResult.Cancel;
            }
        }

        private void frmRegister_Load(object sender, EventArgs e)
        {

        }
        Random rd = new Random();
        //录入信息
        public void MessageAdd()
        {
            try
            {
                if (this.txtid.Text != "" && this.txtpwd.Text != "")
                {
                    db.dbOpen();
                    int numrd = rd.Next(10000000, 100000000);
                    int touxiangnum = rd.Next(0, 4); // 生成0-3的随机头像编号
                    string qqNumber = numrd.ToString();

                    // 确保所有必要字段都有值
                    string sql1 = "insert into QQUsers(QQ,Name,Pwd,phone,imagenum,qianming) " +
                                 "values ('" + numrd.ToString() + "','" + this.txtid.Text + "','" +
                                 this.txtpwd.Text + "','" + this.txtsjh.Text + "','" +
                                 touxiangnum + "','未设置签名')";

                    SqlCommand comm = new SqlCommand(sql1, db.Conn);
                    int num = comm.ExecuteNonQuery();
                    if (num != 0)
                    {
                        MessageBox.Show("注册成功,您的QQ账号为:" + numrd);
                        this.QQNumber = qqNumber;
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show("注册失败");
                    }
                }
                else
                {
                    MessageBox.Show("用户名或者密码不能为空请重新输入");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                db.dbClose();
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            MessageAdd();
        }

    }
}